package com.example.studentsRoom.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.studentsRoom.entity.Building;
import com.example.studentsRoom.entity.Dormitory;
import com.example.studentsRoom.form.SearchForm;
import com.example.studentsRoom.service.impl.DormitoryService;
import com.example.studentsRoom.util.ResultUtil;
import com.example.studentsRoom.vo.PageVo;
import com.example.studentsRoom.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-07-19
 */
@RestController
@RequestMapping("/dormitory")
public class DormitoryController {

    @Autowired
    private DormitoryService dormitoryService;

    /**
     * 查询宿舍
     * @return
     */
    @GetMapping("/availableList/{id}")
    public ResultVo availableList(@PathVariable("id") Integer bid){
        ResultVo resultVo=dormitoryService.availableList(bid);
        return resultVo;
    }

    /**
     * 查询宿舍单个信息
     * @param id
     * @return
     */
    @GetMapping("/list/{id}")
    public ResultVo list(@PathVariable("id") Integer id){
        Dormitory dormitory = dormitoryService.getById(id);
        if (dormitory!=null){
            return ResultUtil.success(dormitory);
        }else{
            return ResultUtil.failed();
        }
    }

    /**
     * 保存
     * @param dormitory
     * @return
     */
    @GetMapping("/save")
    public ResultVo save(Dormitory dormitory){
        boolean save = dormitoryService.save(dormitory);
        if (save) {
            return ResultUtil.success(null);
        }else{
            return ResultUtil.failed();
        }
    }
    /**
     * 分页查询宿舍
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list/{page}/{size}")
    public ResultVo list(
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size
    ){
        PageVo pageVo = dormitoryService.listPage(page, size);
        if (pageVo!=null){
            return ResultUtil.success(pageVo);
        }else return ResultUtil.failed();
    }

    /**
     * 模糊查询+分页
     * @param searchForm
     * @return
     */
    @GetMapping("/search")
    public ResultVo search(SearchForm searchForm){
        PageVo pageVo=dormitoryService.search(searchForm);
        return ResultUtil.success(pageVo);
    }

    /**
     * 更新宿舍信息
     * @param dormitory
     * @return
     */
    @GetMapping("/upDate/{id}")
    public ResultVo upDate(
            @PathVariable("id") Integer id,
            Dormitory dormitory){
        UpdateWrapper<Dormitory> wrapper=new UpdateWrapper<>();
        wrapper.eq("dor_id",id);

        boolean update = dormitoryService.update(dormitory, wrapper);
        if (update){
            return ResultUtil.success(null);
        }else return ResultUtil.failed();
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    public ResultVo deleteById(@PathVariable("id") Integer id){
        boolean b = dormitoryService.removeById(id);
        if (b){
            return ResultUtil.success(null);
        }else{
            return ResultUtil.failed();
        }
    }
}

